package com.example.videosounddemo.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.media.AudioManager
import android.util.Log
import com.example.videosounddemo.App

/**
 *@author: njb
 *@date:  2023/3/27 22:53
 *@desc:
 */
object VolumeChangeHelper {
    private var mContext: Context? = null
    private var mVolumeBroadCastReceiver: MyVolumeReceiver? = null
    private val mVolumeChangeListenerList: MutableList<VolumeChangeListener> = ArrayList()

    private val VOLUME_CHANGE_ACTION:String by lazy {
        "android.media.VOLUME_CHANGED_ACTION"
    }
    private val EXTRA_VOLUME_STREAM_TYPE : String by lazy {
        "android.media.EXTRA_VOLUME_STREAM_TYPE"
    }

    val mAudioManager =
        App.instance.getSystemService(Context.AUDIO_SERVICE) as AudioManager


    fun registerVolumeChangeListener(volumeChangeListener: VolumeChangeListener?) {
        setContext(App.instance)
        if (volumeChangeListener != null && !mVolumeChangeListenerList!!.contains(
                volumeChangeListener
            )
        ) {
            mVolumeChangeListenerList.add(volumeChangeListener)
        }
        mVolumeBroadCastReceiver = MyVolumeReceiver()
        val filter = IntentFilter()
        filter.addAction(VOLUME_CHANGE_ACTION)
        if (mVolumeBroadCastReceiver != null) {
            mContext!!.registerReceiver(mVolumeBroadCastReceiver, filter)
        }
    }

    fun unRegisterReceiver(listener: VolumeChangeListener?) {
        if (listener == null) {
            return
        }
        if (mVolumeChangeListenerList != null && !mVolumeChangeListenerList.isEmpty() &&
            mVolumeChangeListenerList.contains(listener)
        ) {
            mVolumeChangeListenerList.remove(listener)
        }
        if (mVolumeChangeListenerList!!.isEmpty() && mVolumeBroadCastReceiver != null) {
            mContext!!.unregisterReceiver(mVolumeBroadCastReceiver)
        }
    }

    private fun setContext(context: Context) {
        mContext = context
    }

    open class MyVolumeReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            if (action == VOLUME_CHANGE_ACTION && intent.getIntExtra(
                    EXTRA_VOLUME_STREAM_TYPE,
                    -1
                ) == AudioManager.STREAM_MUSIC
            ) {
                val currentVolume: Int = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
                if (currentVolume > 0) {
                    for (listener in mVolumeChangeListenerList) {
                        listener.onVolumeUp(currentVolume)
                    }
                } else if(currentVolume == 0){
                    for (listener in mVolumeChangeListenerList) {
                        listener.onVolumeDownToMin(currentVolume)
                    }
                }
            }
        }

        companion object {
            const val TAG_VOLUME = "VOLUME"
        }
    }

    interface VolumeChangeListener {
        fun onVolumeDownToMin(volume:Int)
        fun onVolumeUp(volume:Int)
    }
}